//
//  MessageView.m
//  享动
//
//  Created by 沈浩hh on 16/4/5.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "MessageView.h"
#import "MessageCollectionViewCell.h"
#import "Message1TableViewController.h"
#import "Message2TableViewController.h"
#import "ScrollViewModel.h"
#import "LBView.h"

@interface MessageView ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *CollectionView;

// 装载最上面的collectionView的title
@property (nonatomic, strong) NSArray *titleArray;

@property (nonatomic, strong) UIView *logoView;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) Message1TableViewController *message1Vc;

@property (nonatomic, strong) Message2TableViewController *message2Vc;


@property (nonatomic, strong) NSMutableArray *scrollViewModelArray;

@property (nonatomic, strong) NSMutableArray *iconArray;

@end


@implementation MessageView
#pragma mark ---- 滚动视图的网络请求 ------
- (void)scrollViewLoadData
{
    _scrollViewModelArray = [NSMutableArray array];
    _iconArray = [NSMutableArray array];
    [RequestManager requestWithUrl:@"http://media.api.thejoyrun.com/slide-list" requestType:RequestTypePost parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *dataArray = dic[@"data"];
        for (NSDictionary *dataDic in dataArray) {
            ScrollViewModel *model = [[ScrollViewModel alloc]init];
            [model setValuesForKeysWithDictionary:dataDic];
            [_scrollViewModelArray addObject:model];
            [_iconArray addObject:model.cover_img];
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            LBView *lbView = [[LBView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, (KHEIGHT - 64 - 30 - 49) / 3)];
            UIView *scView = [lbView addScrollView:CGRectMake(0, 0, KWIDTH, (KHEIGHT - 64 - 30 - 49) / 3) iconArray:_iconArray];
            
            _message1Vc.tableView.tableHeaderView = scView;
            [self.scrollView addSubview:_message1Vc.tableView];
            
        });
        
    } error:^(NSError *error) {
        NSLog(@"错误");
    }];
}

#pragma mark ---- 添加子视图控制器的方法 ------

- (void)creatChildViewController
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64 + 30, KWIDTH, KHEIGHT - 64 - 30 - 5 )];
    self.scrollView.contentSize = CGSizeMake(KWIDTH * _titleArray.count, 0);
    self.scrollView.contentOffset = CGPointMake(0, 0);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces= NO;
    
    _scrollView.showsHorizontalScrollIndicator = NO;
    
    _scrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:_scrollView];
    // 创建推荐视图控制器对象
    // 将控制器的view加给self.view
    
    self.message1Vc = [[Message1TableViewController alloc]init];
    self.message1Vc.view.frame = CGRectMake(0, 0, KWIDTH, KHEIGHT - 64 - 30 - 5);
    [self scrollViewLoadData];
    
    _message2Vc = [[Message2TableViewController alloc]init];
}

- (instancetype)initWithFrame:(CGRect)frame URL:(NSURL *)url
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[NSBundle mainBundle] loadNibNamed:@"MessageView" owner:nil options:nil][0];
        self.frame = [UIScreen mainScreen].bounds;
        
        self.titleArray = @[@"推荐", @"赛事资讯", @"跑步装备", @"提高训练", @"跑步故事", @"跑步文化"];

        [_collectionView registerClass:[MessageCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        // 下划线
        _logoView = [[UIView alloc]initWithFrame:CGRectMake(5, 27, KWIDTH / 5 - 10, 1)];
        _logoView.backgroundColor = [UIColor orangeColor];
        [_collectionView addSubview:_logoView];
        
        // 添加子视图
        [self creatChildViewController];
        
    }
    
    return self;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MessageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.label.text = self.titleArray[indexPath.row];
//    cell.label.backgroundColor = [UIColor redColor];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(KWIDTH / 5, 30);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 4) {
        [_collectionView setContentOffset:CGPointMake((KWIDTH / 5) * 1, 0) animated:YES];
    } else if (indexPath.row == 1) {
        [_collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    _logoView.frame = CGRectMake((KWIDTH / 5) * indexPath.row + 5, 27, KWIDTH / 5 - 10, 1);
    [self.scrollView setContentOffset:CGPointMake(KWIDTH * indexPath.row, 0) animated:YES];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"%f", scrollView.contentOffset.x);
    
    
    // 如果不是集合视图(因为UICollectionView继承于UIScrollView,这个方法对于UICollectionView来说 是重写父类方法)
    if (![scrollView isEqual:_collectionView]) {
        if (scrollView.contentOffset.x / KWIDTH == 3) {
            [_collectionView setContentOffset:CGPointMake((KWIDTH / 5) * (scrollView.contentOffset.x / KWIDTH - 2), 0) animated:YES];
        } else if (scrollView.contentOffset.x / KWIDTH == 2) {
            [_collectionView setContentOffset:CGPointMake((KWIDTH / 5) * (scrollView.contentOffset.x / KWIDTH - 2), 0) animated:YES];
        }
        
        
        _logoView.frame = CGRectMake((KWIDTH / 5) *(scrollView.contentOffset.x / KWIDTH) + 5, 27, KWIDTH / 5 - 10, 1);
    }
}
@end
